package priv.pront.code.national.acwing.search.bfs;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

/**
 * @Description:
 * @Author: pront
 * @Time:2023-05-23 19:30
 */
public class Acwing1106_山峰和山谷 {

    static int n;
    static int[][] g;
    static int[] dx = {-1, -1, 0 , 1, 1, 1, 0, -1}, dy = {0, 1, 1, 1, 0, -1, -1, -1};
    static boolean[][] st;
    static boolean has_higher, has_lower;
    static Queue<int[]> queue = new LinkedList<>();
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        n = scanner.nextInt();
        g = new int[n][n];
        st = new boolean[n][n];
        for(int i = 0; i < n; i++){
            for(int j = 0; j < n; j++){
                g[i][j] = scanner.nextInt();
            }
        }
        int peek = 0;
        int valley = 0;
        for(int i=0;i<n;i++)
            for(int j=0;j<n;j++){
                if(!st[i][j]){
                    has_higher = false;
                    has_lower = false;
                    bfs(i,j);
                    if(!has_higher) peek++;//没有比它高的 山峰+1
                    if(!has_lower) valley++;//没有比它矮的 山谷+1
                }
            }
        System.out.print(peek + " " + valley);
    }

    private static void bfs(int x, int y){
        queue.add(new int[]{x, y});
        st[x][y] = true;
        while(!queue.isEmpty()){
            int[] cur = queue.poll();
            for(int i = 0; i < 8; i++){
                int nx = cur[0] + dx[i], ny = cur[1] + dy[i];
                if(nx < 0 || nx >= n || ny < 0 || ny >= n) continue;
                if(g[nx][ny] != g[cur[0]][cur[1]]){
                    if(g[nx][ny] > g[cur[0]][cur[1]]){
                        has_higher = true;
                    }else{
                        has_lower = true;
                    }
                }else if(!st[nx][ny]){
                    queue.add(new int[]{nx, ny});
                    st[nx][ny] = true;
                }
            }
        }
    }
}
